/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.factory.server.algorithms;

import com.factory.server.dto.EdgeDTO;
import java.util.List;
import org.jgrapht.experimental.dag.DirectedAcyclicGraph;
import org.jgrapht.graph.DefaultEdge;

/**
 * Check if graph is acyclic
 *
 * @author milos
 */
public class CyclicDetector {

    public boolean isAcyclic(List<String> lines) {
        DirectedAcyclicGraph graph = new DirectedAcyclicGraph(DefaultEdge.class);

        for (String s : lines) {
            String[] splittedLine = s.split(" ");
            String sourceNode = splittedLine[0];
            String targetNode = splittedLine[1];
            graph.addVertex(sourceNode);
            graph.addVertex(targetNode);
            try {
                graph.addEdge(sourceNode, targetNode);
            } catch (Exception ex) {
                return false;
            }
        }
        return true;
    }
}
